﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class AnalysisReport_LossCustomers : Frame.Pages.BasePage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        Frame.Global.GlobalFunctions.SetGridView(this.gridView, false);
        if (!IsPostBack && !IsCallback)
        {
            Dictionary<string, SqlParameter> ParamDic = new Dictionary<string, SqlParameter>();
            ParamDic.Add("minDate", new SqlParameter("@minDate", DateTime.Now.AddMonths(-1).ToShortDateString()));
            ParamDic.Add("maxDate", new SqlParameter("@maxDate", DateTime.MaxValue));
            Session["TablesqlParams"] = ParamDic;

            Frame.Global.GlobalFunctions.InitComboBoxColumn(gridView.Columns["t_RefrenceCompany_ID"], "BI_Company", "CompanyName", "CompanyID");
        }
    }

    DataTable table;
    public DataTable Table
    {
        get
        {
            if (Session["TablesqlParams"] != null)
            {
                table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(SqlStrFormat,
                    (Session["TablesqlParams"] as Dictionary<string, SqlParameter>).Values.ToArray());
            }
            return table;
        }
    }

    void DDataBind()
    {
        if (Session["TablesqlParams"] != null)
        {
            gridView.DataSource = Table;
            gridView.DataBind();
        }
    }

    const string SqlStrFormat = @"select a.*,
                                         b.ConsumerDate
                                  from   dbo.BI_Consumer a,
                                         dbo.BLI_ConsumerHis b
                                  where  a.ConsumerID=b.Consumer_ID
                                  and    b.ConsumerDate between @minDate and @maxDate";

    protected void gridView_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.ASPxGridView.GridViewRowType.Data)
        {
            if (e.GetValue("IsVip").ToString() == "1")
            {
                e.Row.BackColor = System.Drawing.Color.Pink;
            }
        }
    }
    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        Dictionary<string, SqlParameter> ParamDic = Session["TablesqlParams"] as Dictionary<string, SqlParameter>;
        switch (Convert.ToInt32(Query_Date.SelectedItem.Value))
        {
            case 0:
                ParamDic["minDate"] = new SqlParameter("@minDate", DateTime.Now.AddMonths(-1));
                ParamDic["maxDate"] = new SqlParameter("@maxDate", DateTime.MaxValue);
                break;
            case 1:
                ParamDic["minDate"] = new SqlParameter("@minDate", DateTime.Now.AddMonths(-3));
                ParamDic["maxDate"] = new SqlParameter("@maxDate", DateTime.MaxValue);
                break;
            case 2:
                ParamDic["minDate"] = new SqlParameter("@minDate", DateTime.Now.AddMonths(-6));
                ParamDic["maxDate"] = new SqlParameter("@maxDate", DateTime.MaxValue);
                break;
            case 3:
                ParamDic["minDate"] = new SqlParameter("@minDate", DateTime.Now.AddYears(-1));
                ParamDic["maxDate"] = new SqlParameter("@maxDate", DateTime.MaxValue);
                break;
            case 4:
                ParamDic["minDate"] = new SqlParameter("@minDate", DateTime.MinValue.AddYears(1900));
                ParamDic["maxDate"] = new SqlParameter("@maxDate", DateTime.Now.AddYears(-1));
                break;
        }
        DDataBind();
    }

    protected override string ActionID
    {
        get { return "0"; }
    }

}